/*
 * Main Class and GUI.
 *
 * Important for creating the applet:
 *
 * 1. create Jar
 *
 * 2. In the HTML add following lines:
 * <html><body>
 * <applet archive="shutdown.jar" code="shutdown.Main" width="150" height="100">
 * </applet>
 * </body></html>
 *
 * 3. In the *.jar-archive add:
 * Main-Class: shutdown.Main
 * Class-Path: shutdown.jar
 *
 * 4.Sign the Jar accordingly to http://www.olison.com/sites/signatur.php
 */
package shutdown;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Jens
 */
public class Main extends JApplet {

	/**
	 * Defines Window
	 * @param title Window Title
	 */
	public Main() {
		Container cp = getContentPane();
		Container sub = new Container();
		cp.setLayout(new BorderLayout());
		sub.setLayout(new BorderLayout());

		JLabel l_time = new JLabel("Time (HHmm, Bsp. 1400): ");
		JTextField tf_time = new JTextField();
		JButton b_start = new JButton("Start");
		JLabel l_status = new JLabel(" ", JLabel.CENTER);
		l_status.setForeground(Color.blue);
		ButtonListener bl = new ButtonListener(b_start, tf_time, l_status);
		b_start.addActionListener(bl);
		tf_time.addKeyListener(new KeyListener(bl));

		sub.add(tf_time, BorderLayout.NORTH);
		sub.add(l_status, BorderLayout.CENTER);
		cp.add(l_time, BorderLayout.NORTH);
		cp.add(sub, BorderLayout.CENTER);
		cp.add(b_start, BorderLayout.SOUTH);

		setVisible(true);
	}

	/**
	 * @param args the command line arguments
	 */
	@Override
	public void init() {
		new Main();
	}
}

/**
 * Reacts on Button Clicks.
 * @author Jens
 */
class ButtonListener implements ActionListener {

	private JButton button;
	private Manager mgr;
	private JTextField time;
	private JLabel status;

	public ButtonListener(JButton button, JTextField time, JLabel status) {
		this.button = button;
		this.time = time;
		this.status = status;
	}

	public void actionPerformed(ActionEvent e) {

		if (button.getText().equals("Start")) {
			this.mgr = new Manager(time.getText());
			/* Try to extract String and shutdown */
			try {
				mgr.shutdown();
			} catch (Exception ex) {
				status.setText(ex.getMessage());
				return;
			}
			/* Change Button title to "Stop" */
			button.setText("Stop");
			status.setText("Shutdown started.");
		} else {
			try {
				mgr.abortShutdown();
			} catch (IOException ex) {
				status.setText(ex.getMessage());
				return;
			}
			button.setText("Start");
			status.setText("Shutdown aborted.");
		}

	}
}

/**
 * Provides an alternative method to start shutdown by
 * hitting the enter-button in the textfield. Uses
 * the actionPerformed() Method from the ButtonListener-class
 * @author Jens
 */
class KeyListener implements java.awt.event.KeyListener {

	private ButtonListener bl;

	public KeyListener(ButtonListener bl) {
		this.bl = bl;
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			bl.actionPerformed(null);
		}
	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}
}
